package com.michaelbulava.TFS.actions;

import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.CommonDataKeys;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.progress.ProgressIndicator;
import com.intellij.openapi.progress.ProgressManager;
import com.intellij.openapi.project.DumbAware;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.vcsUtil.VcsUtil;
import com.michaelbulava.TFS.Messages;
import com.michaelbulava.TFS.TFSVcsModuleComponent;
import com.michaelbulava.TFS.tasks.BackgroundableTask;
import com.michaelbulava.TFS.vcs.TFSVcs;
import com.michaelbulava.TFS.vcs.utils.TFSFileUtil;
import com.microsoft.tfs.core.clients.versioncontrol.soapextensions.Workspace;
import com.microsoft.tfs.util.Check;

import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by MichaelA on 8/20/2015.
 */
public class ReturnOnlineAction extends OnlineRequiredAction {
    /**
     * Implement this method to provide your action handler.
     *
     * @param e Carries information on the invocation place
     */
    @Override
    public void actionPerformed(AnActionEvent e) {
        Project project = (Project)e.getData(CommonDataKeys.PROJECT);
        final VirtualFile[] files = VcsUtil.getVirtualFiles(e);
        List<Module> modules = new ArrayList<>();
        for(VirtualFile file : files){
            Module module = TFSFileUtil.getModuleForFile(file, project);
            if (!modules.contains(module))
                modules.add(module);
        }
        if (!modules.isEmpty()){
            ReturnOnlineTask task = new ReturnOnlineTask(project, modules);
            ProgressManager.getInstance().run(task);
        }
    }

    @Override
    public void update(AnActionEvent e){
        Project project = (Project)e.getData(CommonDataKeys.PROJECT);
        VirtualFile[] files = VcsUtil.getVirtualFiles(e);

        List<Module> modules = new ArrayList<>();
        //Extra check to make sure that the module is controlled by TFS.
        for(VirtualFile file : files){
            Module module = TFSFileUtil.getModuleForFile(file, project);
            if (!modules.contains(module))
                modules.add(module);
        }
        for(Module module : modules){
            TFSVcsModuleComponent moduleComponent = TFSVcsModuleComponent.getInstance(module);

            if (!TFSVcs.isUnderTFS(module.getModuleFile(), project)) {
                e.getPresentation().setEnabled(false);
                return;
            }
        }
        e.getPresentation().setEnabled(!isOnline(project, files));
    }

    private class ReturnOnlineTask extends BackgroundableTask{
        private final List<Module> myModules;

        protected ReturnOnlineTask(Project project, List<Module> modules) {
            super(project, MessageFormat.format(Messages.getString("ReturnOnlineAction.Title_Format"),
                    new Object[]{ modules.size() }), true);

            Check.isTrue(!modules.isEmpty());
            this.myModules = modules;
        }

        @Override
        protected boolean runInBackground() {
            return false;
        }

        @Override
        public void run(ProgressIndicator indicator) {
            for(Module module : myModules){
                TFSVcsModuleComponent moduleComponent = TFSVcsModuleComponent.getInstance(module);
                moduleComponent.setOffline(false);
                moduleComponent.startModuleConnection();
                //TFSVcs.registerModuleForTFS(module, moduleComponent.getTFSUri(), false);

                if (TFSVcs.getInstance(myProject).isModuleOnline(module)){
                    VirtualFile[] files = new VirtualFile[] { VcsUtil.getVirtualFile(myProject.getBasePath()) };
                    DetectLocalChangesAction.DetectLocalChangesTask subTask = new DetectLocalChangesAction
                            .DetectLocalChangesTask(myProject, files);
                    subTask.run(indicator);

                    //Make sure the module component knows it's online
                    moduleComponent.setOffline(false);
                }

            }
        }
    }

}
